package com.michael.demos.base.java.serializable;

import java.io.*;

/**
 * 类功能描述:
 * <pre>
 *   xxxx
 * </pre>
 *
 * @author Michael
 * @version 1.0
 * @date 2020/9/7 14:36
 */
public class SerializableExtendsDemo {

    public static void main(String[] args) {

        subATest();

        subBTest();
    }

    public static void subATest() {
        SubA subA = new SubA();
        subA.setGender("男");
        subA.setAge(11);
        subA.setName("Michael");

        try (
                FileOutputStream out = new FileOutputStream("testA.txt");
                ObjectOutputStream oout = new ObjectOutputStream(out);
                FileInputStream in = new FileInputStream("testA.txt");
                ObjectInputStream oin = new ObjectInputStream(in);
        ) {

            oout.writeObject(subA);

            SubA o = (SubA) oin.readObject();
            System.out.println(o.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void subBTest() {
        SubB subB = new SubB();
        subB.setGender("男");
        subB.setAge(11);
        subB.setName("Michael");

        try (
                FileOutputStream out = new FileOutputStream("testB.txt");
                ObjectOutputStream oout = new ObjectOutputStream(out);
                FileInputStream in = new FileInputStream("testB.txt");
                ObjectInputStream oin = new ObjectInputStream(in);
        ) {

            oout.writeObject(subB);

            SubB o = (SubB) oin.readObject();
            System.out.println(o.toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

class SuperA implements Serializable {

    private static final long serialVersionUID = 1L;

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public SuperA() {
    }

    public SuperA(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "SuperA{" +
               "name='" + name + '\'' +
               ", age=" + age +
               '}';
    }
}


class SuperB {

    private String name;
    private Integer age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public SuperB() {
    }

    public SuperB(String name, Integer age) {
        this.name = name;
        this.age = age;
    }

    @Override
    public String toString() {
        return "SuperA{" +
               "name='" + name + '\'' +
               ", age=" + age +
               '}';
    }
}

class SubA extends SuperA {

    private String gender;

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "SubA{" +
               "gender='" + gender + '\'' +
               "} " + super.toString();
    }
}

class SubB extends SuperB implements Serializable {

    private static final long serialVersionUID = 1L;

    private String gender;

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    @Override
    public String toString() {
        return "SubB{" +
               "gender='" + gender + '\'' +
               "} " + super.toString();
    }
}

